<?php
namespace app\applet\controller\wxapp;

use app\applet\controller\Controller;
use think\facade\View;
use hema\wechat\Driver;

/**
 * 小程序隐私保护管理
 */
class Privacy extends Controller
{
    /**
     * 小程序隐私保护管理
     */
    public function setting()
    {
        $wx = new Driver;
        if (!$this->request->isAjax()) {
            if(!$model = $wx->getPrivacySetting($this->applet_id)){
                $model = ['msg' => $wx->getError()];
            }
            return View::fetch('setting', compact('model'));
        }
        $data = $this->postData('data');
        if(!isset($data['privacy_key']) or sizeof($data['privacy_key']) < 1){
            return $this->renderError('请选择要开启的隐私权限接口');
        }
        $queryarr['owner_setting']['contact_'.$data['contact_type']] = $data['contact'];
        $setting_list = [];
        foreach ($data['privacy_key'] as $item){
            if(empty($data[$item])){
                return $this->renderError('请输入开启《'.$data[$item.'_title'].'》接口目的');
            }
            array_push($setting_list,[
                'privacy_key' => $item,
                'privacy_text' => $data[$item]    
            ]);
        }
        $queryarr['setting_list'] = $setting_list;
		if($result = $wx->setPrivacySetting($this->applet_id,$queryarr)){
		     return $this->renderSuccess('设置成功', url('wxapp.privacy/setting'));
        }
        $error = $wx->getError() ?: '设置失败';
        return $this->renderError($error);
    }
}
